Bootstrap demo

JavaScript Control Statement

Learn about data types, conditional statements, and loops in JavaScript

JavaScript Data Types

JavaScript is a dynamically typed language, meaning variables can hold values of any type without specifying the type explicitly.

Primitive Data Types

// String: textual data
let name = "John Doe";
let message = 'Hello World!';

// Number: integer and floating point numbers
let age = 30;
let temperature = 36.6;

// Boolean: true or false
let isStudent = true;
let isLoggedIn = false;

// Undefined: variable that has not been assigned a value
let undefinedVar;

// Null: intentional absence of any object value
let emptyValue = null;

// Symbol: unique and immutable primitive value
let sym = Symbol('description');

// BigInt: integers larger than 2^53 - 1
let bigNumber = 1234567890123456789012345678901234567890n;

Non-Primitive Data Types

// Object: collection of key-value pairs
let person = {
    name: "Alice",
    age: 25,
    isStudent: true
};

// Array: ordered list of values
let colors = ["red", "green", "blue"];
let numbers = [1, 2, 3, 4, 5];

// Function: callable object
function greet(name) {
    return "Hello, " + name + "!";
}

// Date: represents a specific moment in time
let today = new Date();

Type Checking and Conversion

// Type checking with typeof operator
typeof "Hello";        // "string"
typeof 42;            // "number"
typeof true;         // "boolean"
typeof undefined;    // "undefined"
typeof null;         // "object" (historical bug)

// Type conversion
let numStr = "123";
let num = Number(numStr);  // Convert to number: 123

let numVal = 456;
let strVal = String(numVal); // Convert to string: "456"

let boolVal = Boolean(1); // Convert to boolean: true

Conditional Statements

Conditional statements allow you to execute different code blocks based on different conditions.

if Statement

let age = 18;

// Simple if statement
if (age >= 18) {
    console.log("You are an adult.");
}

// if-else statement
if (age >= 18) {
    console.log("You can vote.");
} else {
    console.log("You are too young to vote.");
}

// if-else if-else statement
if (age < 13) {
    console.log("You are a child.");
} else if (age < 18) {
    console.log("You are a teenager.");
} else {
    console.log("You are an adult.");
}

switch Statement

let day = "Monday";

switch (day) {
    case "Monday":
        console.log("Start of the work week");
        break;
    case "Tuesday":
    case "Wednesday":
    case "Thursday":
        console.log("Midweek days");
        break;
    case "Friday":
        console.log("End of the work week");
        break;
    case "Saturday":
    case "Sunday":
        console.log("Weekend!");
        break;
    default:
        console.log("Invalid day");
}

Ternary Operator

// Syntax: condition ? expressionIfTrue : expressionIfFalse

let age = 20;
let message = age >= 18 ? "Adult" : "Minor";
console.log(message); // "Adult"

// Multiple conditions
let speed = 75;
let warning = speed > 80 ? "Too fast" : speed > 60 ? "Moderate" : "Normal";
console.log(warning); // "Moderate"

Conditional Demo: Check Voting Eligibility

Result will appear here

Loops in JavaScript

Loops allow you to execute a block of code repeatedly until a certain condition is met.

for Loop

// Basic for loop
for (let i = 0; i < 5; i++) {
    console.log("Iteration: " + i);
}

// Looping through an array
let fruits = ["Apple", "Banana", "Cherry"];
for (let i = 0; i < fruits.length; i++) {
    console.log(fruits[i]);
}

// Nested for loops
for (let i = 0; i < 3; i++) {
    for (let j = 0; j < 2; j++) {
        console.log(`i: ${i}, j: ${j}`);
    }
}

while Loop

// while loop - checks condition before execution
let count = 0;
while (count < 5) {
    console.log("Count: " + count);
    count++;
}

// do-while loop - executes at least once
let num;
do {
    num = Math.floor(Math.random() * 10);
    console.log("Random number: " + num);
} while (num !== 5);

for...of and for...in Loops

// for...of loop (for iterables like arrays, strings)
let colors = ["red", "green", "blue"];
for (let color of colors) {
    console.log(color);
}

let str = "Hello";
for (let char of str) {
    console.log(char);
}

// for...in loop (for object properties)
let person = {
    name: "John",
    age: 30,
    occupation: "Developer"
};

for (let key in person) {
    console.log(key + ": " + person[key]);
}

Loop Demo: Multiplication Table Generator

Multiplication table will appear here

Live JavaScript Examples

Data Type Checker

Data type will appear here

Number Classifier

Classification will appear here

Array Iterator

Array iteration will appear here